from enum import Enum
from pydantic import BaseModel, Field

from settings import settings
from common.third_party.save_config import load_config, load_const
from services.new_services.new_server_integration.schemas.sfp_config import SfpConfig
from services.new_services.new_server_integration.schemas.block_configuration import BlockConfig
from services.new_services.new_server_integration.schemas.alarm_configuration import AlarmConfig
from services.new_services.new_server_integration.schemas.calibration_config import CalibrationConfig
from services.server_integration.schemas.full_unit_configuration import FullUnitConfiguration, UnitConfiguration, ServiceChannel, RelayChannel, Modem


def make_cviya() -> str:
    serial_number = load_serial_number_block_type()[0]
    return 'ЦВИЯ.464117.' + serial_number.split(' ')[0]


def make_block_type() -> str:
    return load_serial_number_block_type()[1]


def load_serial_number_block_type() -> (str, str):
    default = FullUnitConfiguration(
        unit_configuration=UnitConfiguration(),
        relay_channels=[RelayChannel(index=idx) for idx in range(settings.SERVICES_VIRTUAL_RELAYS_NUMBER)],
        service_channels=[ServiceChannel(index=idx) for idx in range(settings.SERVICES_MODULE_NUMBER)],
        alarm_config=AlarmConfig(),
        modem=Modem()
    )

    config: FullUnitConfiguration = load_config(
        'si/full_unit_config',
        FullUnitConfiguration,
        load_const(
            'full_unit_config',
            FullUnitConfiguration,
            default
        )
    )

    return config.unit_configuration.main_module.serial_number, config.unit_configuration.unit_type


class BlockType(str, Enum):
    FSR = 'FSR'
    FSC = 'FSC'


class CommonConfiguration(BaseModel):
    block_type: BlockType = Field(default_factory=make_block_type)
    block_cviya: str = Field(default_factory=make_cviya)
    timezone: int = 4
    block_config: BlockConfig = BlockConfig()
    alarm_config: AlarmConfig = AlarmConfig()
    calibration_config: CalibrationConfig = CalibrationConfig()
    sfp_statuses: SfpConfig = SfpConfig()
